/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI.NVQL;

import DAO.CTHoaDonDao;
import GUI.TOOL.ExcelOpener;
import DAO.HoaDonDao;
import DAO.KhachHangDao;
import DAO.NhanVienDao;
import DAO.SanPhamDao;
import GUI.TOOL.XuatExcel;
import POJO.CTHoaDon;
import POJO.HoaDon;
import POJO.KhachHang;
import POJO.SanPham;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Kim
 */
public class frXemDanhSachHoaDonCuaKhachHang extends javax.swing.JInternalFrame {

	private DefaultTableModel tbmodel1;
	private DefaultTableModel tbmodel1s;
	private DefaultTableModel mode2;
	private int TSD;// tong so dong trong bang hoa don
	private int THT; //trang hiện tại
	private int SD;// hien thi bao nhieu dong tron trang
	private int MAKH = 0;// xem hóa đơn của khác hàng này

	public void setMaKhachHang(int maKhachHang) {
		this.MAKH = maKhachHang;
	}

	/**
	 * Creates new form frXemDanhSachHoaDonCuaKhachHang
	 */
	public frXemDanhSachHoaDonCuaKhachHang() {
		initComponents();
		//nhanTen(lbMaKhachHang.getText());
		if (this.MAKH == 0) {
			return;
		}
		load();
	}

	public void nhanTen(String tenKH) {
		lbMaKhachHang.setText(tenKH);
	}

	public void DefaultFormKhachHang() {
		tbmodel1 = new DefaultTableModel();
		tbmodel1s = new DefaultTableModel();

		//tạo các cột
		this.tbmodel1.addColumn("STT ");
		this.tbmodel1.addColumn("Số HĐ");
		this.tbmodel1.addColumn("Trị giá");
		this.tbmodel1.addColumn("Nhân viên phụ trách");

		this.tbmodel1s.addColumn("STT ");
		this.tbmodel1s.addColumn("Số HĐ");
		this.tbmodel1s.addColumn("Ngày lập");
		this.tbmodel1s.addColumn("Trị giá");
		this.tbmodel1s.addColumn("Số tiền giảm");
		this.tbmodel1s.addColumn("Trạng thái");
		this.tbmodel1s.addColumn("Nhân viên phụ trách");
		this.tbmodel1s.addColumn("Mã khách hàng");


	}

	public void load() {
		lbMaKhachHang.setText("  Mã KH:" + Integer.toString(MAKH));

		KhachHang tmp = new KhachHang();
		KhachHangDao khd = new KhachHangDao();
		tmp.setMaKH(MAKH);
		try {
			tmp = khd.layByMa(MAKH);
		} catch (Exception e) {
		}
		if (tmp == null) {
			JOptionPane.showMessageDialog(this, "Khách hàng không tồn tại.", "Thông báo lỗi", JOptionPane.ERROR_MESSAGE);
			DefaultTable2();
			txTenKhachHang.setText("");
			xoaTexBoxTable();
			return;
		}
		if (MAKH == 0) {
			DefaultFormKhachHang();
			xoaTexBoxTable();
			return;
		}
		txTenKhachHang.setText(tmp.getTenKH());
		DefaultFormKhachHang();
		ArrayList<HoaDon> dsHoaDon = null;
		try {
			dsHoaDon = HoaDonDao.layDanhSachHoaDonCuaKhachhang(tmp);

		} catch (Exception e) {
		}

		int i = 1;

		for (HoaDon hd : dsHoaDon) {
			NhanVienDao dao = new NhanVienDao();
			String TenNV = dao.layTenNV(hd.getMaNVPhuTrach());
			Object[] items = new Object[]{
				i,
				hd.getMaHD(),
				hd.getTriGia(),
				TenNV
			};
			Object[] items2 = new Object[]{
				i,
				hd.getMaHD(),
				hd.getNgayLap(),
				hd.getTriGia(),
				hd.getSoTienGiam(),
				hd.getTrangThai(),
				hd.getMaNVPhuTrach(),
				hd.getMaKH()
			};
			i++;
			//int maLSP = sp.getMaLSP();
			tbmodel1.addRow(items);
			tbmodel1s.addRow(items2);

		}
		jtHoaDon.setModel(tbmodel1);

		int[] va = {10,15, 30, 45};
		int[] pt = new int[4];
		for (int l = 0; l < 4; l++) {
			pt[l] = jtHoaDon.getWidth() * va[l] / 100;
			jtHoaDon.getColumnModel().getColumn(l).setPreferredWidth(pt[l]);

		}
		int soDong = tbmodel1.getRowCount();
		if (soDong != 0) {
			loadCTHD(0);
		} else {
			xoaTexBoxTable();
		}
	}

	/**
	 * This method is called from within the constructor to initialize the
	 * form. WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
       // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
       private void initComponents() {

              jPanel1 = new javax.swing.JPanel();
              jLabel1 = new javax.swing.JLabel();
              lbMaKhachHang = new javax.swing.JLabel();
              jPanel3 = new javax.swing.JPanel();
              jPanel4 = new javax.swing.JPanel();
              jScrollPane2 = new javax.swing.JScrollPane();
              jtHoaDon = new javax.swing.JTable();
              jPanel6 = new javax.swing.JPanel();
              jPanel8 = new javax.swing.JPanel();
              jLabel3 = new javax.swing.JLabel();
              jtfMaHD = new javax.swing.JTextField();
              jLabel4 = new javax.swing.JLabel();
              jtfNgayLap = new javax.swing.JTextField();
              jLabel5 = new javax.swing.JLabel();
              jtfTriGia = new javax.swing.JTextField();
              jLabel6 = new javax.swing.JLabel();
              jtfSoTienGIam = new javax.swing.JTextField();
              jLabel7 = new javax.swing.JLabel();
              jtfTrangThai = new javax.swing.JTextField();
              jLabel8 = new javax.swing.JLabel();
              jtfTenNV = new javax.swing.JTextField();
              jButton2 = new javax.swing.JButton();
              jLabel10 = new javax.swing.JLabel();
              jLabel11 = new javax.swing.JLabel();
              jPanel7 = new javax.swing.JPanel();
              jScrollPane1 = new javax.swing.JScrollPane();
              jtCTHoaDon = new javax.swing.JTable();
              txTenKH = new javax.swing.JPanel();
              jbtnLapBaoCao = new javax.swing.JButton();
              txMaKH = new javax.swing.JTextField();
              jLabel2 = new javax.swing.JLabel();
              jLabel9 = new javax.swing.JLabel();
              txTenKhachHang = new javax.swing.JTextField();

              setClosable(true);
              setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
              setIconifiable(true);
              setMaximizable(true);
              setResizable(true);

              jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
              jLabel1.setText("XEM DANH SÁCH HÓA ĐƠN CỦA KHÁCH HÀNG");

              lbMaKhachHang.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N

              javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
              jPanel1.setLayout(jPanel1Layout);
              jPanel1Layout.setHorizontalGroup(
                     jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel1Layout.createSequentialGroup()
                            .addContainerGap(65, Short.MAX_VALUE)
                            .addComponent(jLabel1)
                            .addGap(18, 18, 18)
                            .addComponent(lbMaKhachHang, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addContainerGap(65, Short.MAX_VALUE))
              );
              jPanel1Layout.setVerticalGroup(
                     jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                   .addComponent(lbMaKhachHang, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                                   .addComponent(jLabel1))
                            .addGap(0, 0, Short.MAX_VALUE))
              );

              jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Danh sách hóa đơn"));
              jPanel4.setToolTipText("");

              jtHoaDon.setModel(new javax.swing.table.DefaultTableModel(
                     new Object [][] {

                     },
                     new String [] {
                            "STT", "Số HĐ", "Trị giá", "Nhân viên phụ trách"
                     }
              ));
              jtHoaDon.addMouseListener(new java.awt.event.MouseAdapter() {
                     public void mouseClicked(java.awt.event.MouseEvent evt) {
                            jtHoaDonMouseClicked(evt);
                     }
              });
              jScrollPane2.setViewportView(jtHoaDon);

              javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
              jPanel4.setLayout(jPanel4Layout);
              jPanel4Layout.setHorizontalGroup(
                     jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 428, Short.MAX_VALUE)
              );
              jPanel4Layout.setVerticalGroup(
                     jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
              );

              jPanel6.setBorder(javax.swing.BorderFactory.createTitledBorder("Thông tin hóa đơn"));

              jLabel3.setText("Mã hóa đơn:");

              jtfMaHD.setEditable(false);

              jLabel4.setText("Ngày lập:");

              jtfNgayLap.setEditable(false);

              jLabel5.setText("Trị giá:");

              jtfTriGia.setEditable(false);

              jLabel6.setText("Số tiền giảm:");

              jtfSoTienGIam.setEditable(false);

              jLabel7.setText("Trạng thái:");

              jtfTrangThai.setEditable(false);

              jLabel8.setText("Nhân viên bán:");

              jtfTenNV.setEditable(false);

              jButton2.setText("Lưu ra Excel");
              jButton2.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            jButton2ActionPerformed(evt);
                     }
              });

              jLabel10.setText("VNĐ");

              jLabel11.setText("VNĐ");

              javax.swing.GroupLayout jPanel8Layout = new javax.swing.GroupLayout(jPanel8);
              jPanel8.setLayout(jPanel8Layout);
              jPanel8Layout.setHorizontalGroup(
                     jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel8Layout.createSequentialGroup()
                            .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                   .addGroup(jPanel8Layout.createSequentialGroup()
                                          .addGap(28, 28, 28)
                                          .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                 .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)
                                                 .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING)
                                                 .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING)
                                                 .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING)
                                                 .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.TRAILING)
                                                 .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.TRAILING))
                                          .addGap(18, 18, 18)
                                          .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                 .addComponent(jtfTrangThai, javax.swing.GroupLayout.DEFAULT_SIZE, 231, Short.MAX_VALUE)
                                                 .addComponent(jtfTenNV, javax.swing.GroupLayout.Alignment.TRAILING)
                                                 .addComponent(jtfMaHD)
                                                 .addComponent(jtfNgayLap, javax.swing.GroupLayout.Alignment.TRAILING)
                                                 .addGroup(jPanel8Layout.createSequentialGroup()
                                                        .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                                               .addComponent(jtfSoTienGIam, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE)
                                                               .addComponent(jtfTriGia, javax.swing.GroupLayout.Alignment.LEADING))
                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                        .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                               .addComponent(jLabel10)
                                                               .addComponent(jLabel11))
                                                        .addGap(0, 0, 0))))
                                   .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel8Layout.createSequentialGroup()
                                          .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                          .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
              );
              jPanel8Layout.setVerticalGroup(
                     jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel8Layout.createSequentialGroup()
                            .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                   .addComponent(jLabel3)
                                   .addComponent(jtfMaHD, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                   .addComponent(jLabel4)
                                   .addComponent(jtfNgayLap, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                   .addComponent(jLabel5)
                                   .addComponent(jtfTriGia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                   .addComponent(jLabel10))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                   .addComponent(jLabel6)
                                   .addComponent(jtfSoTienGIam, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                   .addComponent(jLabel11))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                   .addComponent(jLabel7)
                                   .addComponent(jtfTrangThai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                   .addComponent(jtfTenNV, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                   .addComponent(jLabel8))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 8, Short.MAX_VALUE)
                            .addComponent(jButton2))
              );

              jPanel7.setBorder(javax.swing.BorderFactory.createTitledBorder("Chi tiết hóa đơn"));

              jtCTHoaDon.setModel(new javax.swing.table.DefaultTableModel(
                     new Object [][] {

                     },
                     new String [] {
                            "STT", "Tên sản phẩm", "Số lượng", "Giá bán", "Thành tiền"
                     }
              ));
              jScrollPane1.setViewportView(jtCTHoaDon);

              javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
              jPanel7.setLayout(jPanel7Layout);
              jPanel7Layout.setHorizontalGroup(
                     jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
              );
              jPanel7Layout.setVerticalGroup(
                     jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 156, Short.MAX_VALUE)
              );

              javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
              jPanel6.setLayout(jPanel6Layout);
              jPanel6Layout.setHorizontalGroup(
                     jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addComponent(jPanel8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                     .addComponent(jPanel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
              );
              jPanel6Layout.setVerticalGroup(
                     jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel6Layout.createSequentialGroup()
                            .addComponent(jPanel8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(0, 0, 0)
                            .addComponent(jPanel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
              );

              javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
              jPanel3.setLayout(jPanel3Layout);
              jPanel3Layout.setHorizontalGroup(
                     jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel3Layout.createSequentialGroup()
                            .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
              );
              jPanel3Layout.setVerticalGroup(
                     jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                     .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
              );

              jbtnLapBaoCao.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
              jbtnLapBaoCao.setText("Lập báo cáo");
              jbtnLapBaoCao.setToolTipText("gọi lập phiếu nhập kho");
              jbtnLapBaoCao.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            jbtnLapBaoCaoActionPerformed(evt);
                     }
              });

              jLabel2.setText("Nhập mã khách hàng:");

              jLabel9.setText("Tên khách hàng:");

              txTenKhachHang.setEditable(false);
              txTenKhachHang.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            txTenKhachHangActionPerformed(evt);
                     }
              });

              javax.swing.GroupLayout txTenKHLayout = new javax.swing.GroupLayout(txTenKH);
              txTenKH.setLayout(txTenKHLayout);
              txTenKHLayout.setHorizontalGroup(
                     txTenKHLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, txTenKHLayout.createSequentialGroup()
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(txTenKHLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                   .addComponent(jLabel2)
                                   .addComponent(jLabel9))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(txTenKHLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                   .addComponent(txTenKhachHang, javax.swing.GroupLayout.DEFAULT_SIZE, 187, Short.MAX_VALUE)
                                   .addComponent(txMaKH))
                            .addGap(18, 18, 18)
                            .addComponent(jbtnLapBaoCao)
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
              );
              txTenKHLayout.setVerticalGroup(
                     txTenKHLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, txTenKHLayout.createSequentialGroup()
                            .addGroup(txTenKHLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                   .addComponent(txMaKH, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                   .addComponent(jLabel2))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(txTenKHLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                   .addComponent(txTenKhachHang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                   .addComponent(jLabel9)
                                   .addComponent(jbtnLapBaoCao))
                            .addGap(0, 0, 0))
              );

              javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
              getContentPane().setLayout(layout);
              layout.setHorizontalGroup(
                     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                   .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                   .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                   .addComponent(txTenKH, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGap(0, 0, 0))
              );
              layout.setVerticalGroup(
                     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(layout.createSequentialGroup()
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(0, 0, 0)
                            .addComponent(txTenKH, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(0, 0, 0)
                            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGap(0, 0, 0))
              );

              pack();
       }// </editor-fold>//GEN-END:initComponents

       private void jbtnLapBaoCaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnLapBaoCaoActionPerformed
		// TODO add your handling code here:
		int ma = 0;
		try {
			ma = Integer.parseInt(txMaKH.getText().trim());
			this.MAKH = ma;
		} catch (NumberFormatException e) {
			JOptionPane.showMessageDialog(this, "Nhập số", "Thông báo lỗi", JOptionPane.ERROR_MESSAGE);
			txMaKH.requestFocus();
			txMaKH.selectAll();;
			return;
		}
		load();
       }//GEN-LAST:event_jbtnLapBaoCaoActionPerformed

    private void jtHoaDonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtHoaDonMouseClicked
	    // TODO add your handling code here:

	    int dong = this.jtHoaDon.getSelectedRow();
	    if (dong != -1) {
		    loadCTHD(dong);
	    } else {
		    xoaTexBoxTable();
	    }
    }//GEN-LAST:event_jtHoaDonMouseClicked

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
	    // TODO add your handling code here:
	    try {
		    //     Note that i'm actually saving the file first
		    XuatExcel exp = new XuatExcel();
		    File file = new File("BaoCaoHoaDon.xls");

		    exp.exportTable(jtHoaDon, file);

		    ExcelOpener opn = new ExcelOpener();
		    opn.openTable(file);
	    } catch (IOException ex) {
		    System.out.println(ex.getMessage());
		    ex.printStackTrace();
	    }
    }//GEN-LAST:event_jButton2ActionPerformed

       private void txTenKhachHangActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txTenKhachHangActionPerformed
		// TODO add your handling code here:
       }//GEN-LAST:event_txTenKhachHangActionPerformed
	private void xoaTexBoxTable() {
		jtfMaHD.setText("");
		jtfNgayLap.setText("");
		jtfSoTienGIam.setText("");
		jtfTenNV.setText("");
		jtfTriGia.setText("0.0");
		jtfTrangThai.setText("");

		DefaultTable2();
	}

	private void DefaultTable2() {
		String[] columns = new String[]{"STT", "Tên sản phẩm", "Số lượng", "Giá bán", "Thành tiền"};
		mode2 = new DefaultTableModel(null, columns);
		jtCTHoaDon.setModel(mode2);
		int[] va = {10, 30, 10, 25, 25};
		int[] pt = new int[5];
		for (int l = 0; l < 5; l++) {
			pt[l] = jtCTHoaDon.getWidth() * va[l] / 100;
			jtCTHoaDon.getColumnModel().getColumn(l).setPreferredWidth(pt[l]);

		}
	}

	public void loadCTHD(int dong) {
		// load tren text
		int maHD = Integer.parseInt(this.tbmodel1s.getValueAt(dong, 1).toString());
		String NgayLap = (String) this.tbmodel1s.getValueAt(dong, 2).toString();
		Float soTienGiam = Float.parseFloat(this.tbmodel1s.getValueAt(dong, 4).toString());
		int trangThai = Integer.parseInt(this.tbmodel1s.getValueAt(dong, 5).toString());
		int maNV = Integer.parseInt(this.tbmodel1s.getValueAt(dong, 6).toString());

		jtfMaHD.setText(String.valueOf(maHD));
		jtfNgayLap.setText(NgayLap);
		jtfSoTienGIam.setText(String.valueOf(soTienGiam));
		jtfTrangThai.setText(String.valueOf(trangThai));
		NhanVienDao nv = new NhanVienDao();
		jtfTenNV.setText(String.valueOf(nv.layTenNV(maNV)));

		// load luoi chi tiet 

		SanPhamDao dao = new SanPhamDao();
		SanPham sp = new SanPham();
		ArrayList<CTHoaDon> dsCTPNhapKho = CTHoaDonDao.layDSByMaHD(maHD);
		DefaultTable2();
		double gia = 0;
		int t = 1;
		for (CTHoaDon cthd : dsCTPNhapKho) {
			sp = dao.LaySanPhamByMa(cthd.getMaSP());
			Object[] items = new Object[]{
				t,
				//ctpnk.getMaSP(),
				sp.getTenSP(),
				cthd.getSoLuong(),
				cthd.getGiaBan(),
				cthd.getSoLuong() * cthd.getGiaBan()
			};
			t++;
			mode2.addRow(items);
			gia += Double.valueOf(cthd.getGiaBan());
		}
		jtfTriGia.setText(Double.toString(gia));

	}
       // Variables declaration - do not modify//GEN-BEGIN:variables
       private javax.swing.JButton jButton2;
       private javax.swing.JLabel jLabel1;
       private javax.swing.JLabel jLabel10;
       private javax.swing.JLabel jLabel11;
       private javax.swing.JLabel jLabel2;
       private javax.swing.JLabel jLabel3;
       private javax.swing.JLabel jLabel4;
       private javax.swing.JLabel jLabel5;
       private javax.swing.JLabel jLabel6;
       private javax.swing.JLabel jLabel7;
       private javax.swing.JLabel jLabel8;
       private javax.swing.JLabel jLabel9;
       private javax.swing.JPanel jPanel1;
       private javax.swing.JPanel jPanel3;
       private javax.swing.JPanel jPanel4;
       private javax.swing.JPanel jPanel6;
       private javax.swing.JPanel jPanel7;
       private javax.swing.JPanel jPanel8;
       private javax.swing.JScrollPane jScrollPane1;
       private javax.swing.JScrollPane jScrollPane2;
       private javax.swing.JButton jbtnLapBaoCao;
       private javax.swing.JTable jtCTHoaDon;
       private javax.swing.JTable jtHoaDon;
       private javax.swing.JTextField jtfMaHD;
       private javax.swing.JTextField jtfNgayLap;
       private javax.swing.JTextField jtfSoTienGIam;
       private javax.swing.JTextField jtfTenNV;
       private javax.swing.JTextField jtfTrangThai;
       private javax.swing.JTextField jtfTriGia;
       public javax.swing.JLabel lbMaKhachHang;
       private javax.swing.JTextField txMaKH;
       private javax.swing.JPanel txTenKH;
       private javax.swing.JTextField txTenKhachHang;
       // End of variables declaration//GEN-END:variables
}
